﻿#-*- coding: utf-8 -*-

class CursorReaderBase(object):
	''''''
	def __inti__(self, *args):
		pass

class ConnectionBase(object):
	''''''
	def connect(self):
		raise Exception('Not implemented.')
	
	def create_database(self):
		pass
	
	def safecheck(self, obj):
		if obj is None:
			return ''
		if isinstance(obj, int) or isinstance(obj, float):
			return '%s' % obj
		if isinstance(obj, str) or isinstance(obj, unicode):
			return "'%s'" % obj.replace("'", "''")
		return self.safecheck('%s' % obj)
	
	def build_sql(self, *args):
		if len(args)>0:
			sql = args[0]
			if len(args)>1:
				parameters = args[1:]
				safe_parameters = []
				valueindex = 0
				for p in parameters:
					if p is None:
						raise Exception('"None" value found at index %s.' % valueindex)
					safe_parameters.append(self.safecheck(p))
					valueindex += 1
				sql = sql % tuple(safe_parameters)
			return sql
		return ''
	
	def read(self, *args):
		sql = self.build_sql(*args)
		if len(sql)>0:
			reader = self.execute_reader(sql)
			result = []
			while reader.read():
				result.append(reader.row)
			return result
		return []
	
	def execute_reader(self, sql):
		raise Exception('Not implemented.')
	
	def execute(self, *args):
		raise Exception('Not implemented.')
	
	@property
	def lastrowid(self):
		raise Exception('Not implemented.')
	
	def get_tables(self):
		raise Exception('Not implemented.')
	
	def close(self):
		raise Exception('Not implemented.')
